const path = require('path')
const winston = require('winston')
const mkdirp = require('mkdirp')
const _ = require('lodash')


const levelUpperFormat = winston.format((info, opts) => {
    info.level = info.level.toUpperCase()
    return info
})

const printfFormat = winston.format.printf(info => `${info.timestamp} [${info.level}] [${_.defaultTo(info.label, 'N')}]: ${info.message}`)


module.exports = ({logDir, logFileName, consoleLogLevel, logLevel}) => {
    mkdirp.sync(logDir);

    const logFile = path.join(logDir, logFileName)

    return winston.createLogger({
        level: consoleLogLevel,
        transports: [
            new winston.transports.Console({
                level: consoleLogLevel,
                format: winston.format.combine(
                    winston.format.timestamp({
                        format: 'YYYY-MM-DD HH:mm:ss'
                    }),
                    levelUpperFormat(),
                    winston.format.colorize(),
                    winston.format.splat(),
                    printfFormat,
                ),
            }),
            new winston.transports.File({
                level: logLevel,
                filename: logFile,
                format: winston.format.combine(
                    winston.format.timestamp({
                        format: 'YYYY-MM-DD HH:mm:ss'
                    }),
                    levelUpperFormat(),
                    winston.format.splat(),
                    printfFormat,
                )
            }),
        ]
    })
}